package org.m2acsi.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class PrettyDate {

    public static String timeLeft(Date date) {
        Calendar d = Calendar.getInstance();
        d.setTime(date);
        Calendar t = Calendar.getInstance();

        long dms = d.getTimeInMillis();
        long tms = t.getTimeInMillis();
        long deltams = tms - dms;
        long hours = Math.abs(deltams / (60 * 60 * 1000));
        long minutes = Math.abs(deltams / (60 * 1000));

        switch ((int) hours) {
            case 0:
                return new StringBuilder().append(minutes).append(" minute").append(minutes > 1 ? "s" : "").toString();
            case 1:
                return "1 hour";
            default:
                return new StringBuilder().append(hours).append(" hours").toString();
        }
    }

    public static String niceCreation(Date date) {
        Calendar d = Calendar.getInstance();
        d.setTime(date);
        Calendar t = Calendar.getInstance();

        if (Math.abs((t.getTimeInMillis() - d.getTimeInMillis()) / (60 * 60 * 1000)) > 48) {
            return transform(date);
        } else {
            return timeLeft(date) + " ago";
        }
    }

    public static String transform(Date date) {
        Calendar d = Calendar.getInstance();
        d.setTime(date);
        Calendar t = Calendar.getInstance();

        if (d.get(Calendar.YEAR) == t.get(Calendar.YEAR)
                && d.get(Calendar.MONTH) == t.get(Calendar.MONTH)
                && d.get(Calendar.DAY_OF_YEAR) == t.get(Calendar.DAY_OF_YEAR)) {
            //Same day
            if (date.compareTo(new Date()) <= 0) {
                return "Today";
            } else {
                return new StringBuilder().append("Today at ").append((new SimpleDateFormat("HH:mm")).format(date)).toString();
            }
        } else {
            long dms = d.getTimeInMillis();
            long tms = t.getTimeInMillis();
            long deltams = tms - dms;
            long deltaDays = Math.abs(deltams / (24 * 60 * 60 * 1000));
            if (tms > dms) {
                //passed
                d.add(Calendar.DAY_OF_YEAR, -1);
                if (d.get(Calendar.YEAR) == t.get(Calendar.YEAR)
                        && d.get(Calendar.MONTH) == t.get(Calendar.MONTH)
                        && d.get(Calendar.DAY_OF_YEAR) == t.get(Calendar.DAY_OF_YEAR)) {
                    return "Yesterday";
                } else {
                    if (deltaDays <= 1) {
                        return "Yesterday";
                    } else {
                        return new StringBuilder().append(deltaDays).append(" days ago").toString();
                    }
                }
            } else {
                //future
                deltaDays++;
                d.add(Calendar.DAY_OF_YEAR, 1);
                if (d.get(Calendar.YEAR) == t.get(Calendar.YEAR)
                        && d.get(Calendar.MONTH) == t.get(Calendar.MONTH)
                        && d.get(Calendar.DAY_OF_YEAR) == t.get(Calendar.DAY_OF_YEAR)) {
                    return "Tomorrow";
                } else {
                    if (deltaDays <= 1) {
                        return "Tomorrow";
                    } else {
                        return new StringBuilder().append("In ").append(deltaDays).append(" days").toString();
                    }
                }
            }
        }
    }

    public static long rawTimeLeft(Date date) {
        Calendar d = Calendar.getInstance();
        d.setTime(date);
        Calendar t = Calendar.getInstance();

        return Math.abs(t.getTimeInMillis() - d.getTimeInMillis());
    }
}
